package com.example.doma.service;

import com.example.doma.mapper.dormMapper;
import com.example.doma.mapper.studentMapper;
import com.example.doma.pojo.dorm;
import com.example.doma.pojo.student;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class dormService {

    @Autowired
    private dormMapper dormMapper;

@Autowired
   private  studentService studentService;

    public PageInfo<dorm> queryAllPages(Integer pageNum,Integer pagesize)
    {
        PageHelper.startPage(pageNum,pagesize);
        List<dorm> dorms = this.dormMapper.getdorms();
        PageInfo<com.example.doma.pojo.dorm> dormPageInfo = new PageInfo<>(dorms, pagesize);
          if(dormMapper.getpage()%pagesize>0)
              dormPageInfo.setNavigatePages(Integer.valueOf(dormMapper.getpage()/pagesize+1));
      else
              dormPageInfo.setNavigatePages(Integer.valueOf(dormMapper.getpage()/pagesize));

        return dormPageInfo;

    }
    public  List<dorm> getdorms()
    {
        List<dorm> dorms = dormMapper.getdorms();
        return  dorms;
    }
    public  int addDorm(dorm dorm)
    {
        dormMapper.adddorm(dorm);
        return 1;
    }

    public boolean isdorm(String apart_num ,int dormi_num)
    {
        if(dormMapper.getdormByapart_numAnddormi_num(apart_num,dormi_num)==null)
        {
            return false;
        }
        else
            return true;
    }

    public  dorm  getdrom(String apart_num ,int dormi_num )
    {
        dorm dorm = dormMapper.getdormByapart_numAnddormi_num(apart_num, dormi_num);
        return  dorm;
    }
    public  int updatedorm(dorm dorm)
    {
        dormMapper.update(dorm);
        return 1;

    }
    public  List<dorm> getDormByapart_numAnddormi_num(String apart_num,int dormi_num)
    {
       dorm dor = dormMapper.getdormByapart_numAnddormi_num(apart_num, dormi_num);
        List<dorm> objects = new ArrayList<dorm>();
        objects.add(dor);
        return  objects ;
    }
    public int  deledorm(String apart_num,int  dormi_num)
    {
        dormMapper.deletedormByapart_numAnddormi_num(apart_num,dormi_num);
        return 1;
    }
//获取同班同学的寝室
    public List<dorm> getdormsByspec_nameAndclass_nameAndgrade(String spec_name,String class_name,int grade,int sex)
    {
        List<dorm> dorms = dormMapper.getdormsByspec_nameAndclass_nameAndgrade(spec_name, class_name, grade, sex);
        return dorms;
    }
//获取同专业同学
    public List<dorm> getdormsByspec_nameAndgrade(String spec_name, int grade,int sex,String class_name)
    {
        List<dorm> dorms = dormMapper.getdormsByspec_nameAndgrade(spec_name, grade, sex,class_name);
        return dorms;
    }
    //获取同院级学生
    public List<dorm> getdormsByfaculty_nameAndBygrade(String faculty_name,int grade,int sex,String spec_name)
    {
        List<dorm> dorms = dormMapper.getdormsByfaculty_nameAndBygrade(faculty_name, grade, sex,spec_name);
        return dorms;
    }
    //获取宿舍空余床位
    public int[] getBednum(String apart_num,int dormi_num)
    {
        dorm dorm = dormMapper.getdormByapart_numAnddormi_num(apart_num, dormi_num);
        int[] bednums = new int[dorm.getMax_num()+1];
        List<student> students = studentService.getstudentsBydorm(apart_num, dormi_num);
        for (student st:students)
        {
            //如果宿舍床号有人，数组标记 1
            bednums[st.getBed_num()]=1;
        }
        System.out.println("寝室床位情况");
        for (int i=1;i<bednums.length;i++)
            System.out.println(i+"床"+bednums[i]);
        return  bednums;

    }
    //判断床位是否存在
    public  boolean ishavabed_num(String apart_num,int dormi_num,int bed_num)
    {
        int[] bed_nums = getBednum(apart_num, dormi_num);
        //如果床位有人返回 true
        if(bed_nums[bed_num]==1)
            return true;

        return false;
    }

    //批量添加宿舍功能
    public void  AddDomas(HSSFWorkbook workbook)
    {
        //获取工作表
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        System.out.println("获取公寓信息：");
        List<dorm> dorms=new ArrayList<>();
        System.out.println("公寓参数：");
        //公寓参数
        HSSFRow row1 = sheet.getRow(0);
        for (int i=0;i<row1.getLastCellNum();i++)
        {
            HSSFCell cell = row1.getCell(i);
              cell.setCellType(CellType.STRING);
            System.out.print(cell.getStringCellValue()+"  ");
        }
        System.out.println();
       List<String> dor =new ArrayList<>();
        for (int i=1;i<lastRowNum;i++)
        {
            //获取指定行
            HSSFRow row = sheet.getRow(i);
            if (row==null)
                continue;

            for (int j=0;j<row.getLastCellNum();j++)
            {

                HSSFCell cell = row.getCell(j);
                if(cell==null)
                    continue;
                cell.setCellType(CellType.STRING);
               dor.add(j,cell.getStringCellValue());
                System.out.print(cell.getStringCellValue()+"  ");

            }
            dorms.add(new dorm(dor.get(0),Integer.valueOf(dor.get(1)),Integer.valueOf(dor.get(2)),Integer.valueOf(dor.get(3)),Integer.valueOf(dor.get(4)),dor.get(5)));

            System.out.println("");
        }
        System.out.println(dorms);
        dormMapper.adddorms(dorms);
    }

}
